package com.janko.sccommon.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author nyk
 * @Date 2021/3/30 15:16
 * @Version 1.0
 * @Desc
 */
public class FormatDateUtil {
    public static void main(String[] args) {
        long l = convert2DateTimelong(null);
    }

    public static Long convert2DateTimelong(String date) {
        int i = 0;
        long timeValue = 0;
        try {
            if (!org.springframework.util.StringUtils.isEmpty(date)) {
                if (date.contains("T")) {
                    String[] ts = StringUtils.split(date, ".");
                    String t = ts[0].replace("T", " ");
                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    timeValue = sf.parse(t).getTime() + 28800000;
                } else {
                    if (date.length() > 10) {
                        //判断冒号的出现次数
                        int count = date.split(":").length - 1;
                        if (date.contains("-")) {
                            if (count == 2) {
                                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                timeValue = sf.parse(date).getTime();
                            } else {
                                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
                                timeValue = sf.parse(date).getTime();
                            }
                        } else {
                            if (count == 2) {
                                SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                                timeValue = sf.parse(date).getTime();
                            } else {
                                SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
                                timeValue = sf.parse(date).getTime();
                            }
                        }
                    } else {
                        if (date.contains("-")) {
                            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                            timeValue = sf.parse(date).getTime();
                        } else {
                            SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
                            timeValue = sf.parse(date).getTime();
                        }
                    }
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            i = -1;
            e.printStackTrace();
        }
        if (i == -1) {
            return -1L;
        } else {
            return timeValue;
        }

    }

    public static String convert2DateTimeFormat(Long dateString) {
        Date date = new Date(dateString);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String convert2DateFormat(Long dateString) {

        Date date = new Date(dateString);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    /**
     * @param dateString 时间
     * @param formatStr  转化格式
     * @return
     */
    public static String convert2DateFormat(Long dateString, String formatStr) {
        Date date = new Date(dateString);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        return simpleDateFormat.format(date);
    }

    /**
     * 去掉时间最后的毫秒
     *
     * @return
     */
    public static Long removeMilliseconds(String dateStr) {
        if (!org.springframework.util.StringUtils.isEmpty(dateStr)) {
            Long date = Long.parseLong(dateStr);
            date = date - date % 1000;
            return date;
        }
        return null;
    }

    /**
     * 将日期字符串转为13位时间戳
     * @param dataStr 日期格式包涵以下几种情况
     *                *YYYY年MM月dd日  *YYYY年M月d日
     *                *2012/3/14， 2012/3/14
     *                2012-03-14，2012-3-4
     * @return 13位时间戳
     */
    public static String DataStrToTimeMillis(String dataStr) throws ParseException {
        String result="";
        if(StringUtils.isEmpty(dataStr)) return result;
        if(dataStr.startsWith("*"))
            dataStr=dataStr.substring(1);
        String formatPatter = "";
        if(dataStr.contains("年")){
            formatPatter = "yyyy年MM月dd日";
        }else if(dataStr.contains("/")){
            formatPatter = "yyyy/MM/dd";
        }else if(dataStr.contains("-")){
            formatPatter = "yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(formatPatter);
        Date date = format.parse(dataStr);
        String dateStr  = String.valueOf(date.getTime());
        return dateStr;
    }
}
